import pygame, Utilities,  math
from pygame.locals import *
from Utilities import *

class Shoot(pygame.sprite.Sprite):
    
    def __init__(self,  player,  shoot_speed,  screen_width):
        pygame.sprite.Sprite.__init__(self)
        self.SHOOT_SPEED = shoot_speed
        self.SCREENWIDTH = screen_width
        self.image = load_image("ball.jpg",  -1)
        self.image = pygame.transform.scale(self.image,  (25, 25))
        self.angle = player.rotateAngle
        self.rotateAngle = math.radians(self.angle)
        self.rect = Rect(0,  0, 25,  25)
        self.rect.center = (player.centerX,  player.centerY)
        self.centerX = player.centerX
        self.centerY = player.centerY
        
    def update(self):
        # Si el disparo se fue por la parte superior de la pantalla entonces lo elimino
        if self.rect.bottom <= 0: 
            self.kill()
        
        nextX,  nextY = (self.rect.centerx,  self.rect.centery)
        if self.rotateAngle > 0:
            nextX =  - math.fabs(math.sin(self.rotateAngle) * self.SHOOT_SPEED)
        else:
            nextX =  math.fabs(math.sin(self.rotateAngle) * self.SHOOT_SPEED)
        nextY =  - math.fabs(math.cos(self.rotateAngle) * self.SHOOT_SPEED)
        self.rect.move_ip(nextX,  nextY)
        
        # Si el disparo se va por un lateral, entonces rebota
        if self.rect.bottomleft[0] >= self.SCREENWIDTH or self.rect.bottomright[0] <= 0:
            self.angle = -self.angle
            self.rotateAngle = math.radians(self.angle)
        
